package cn.zd.erp.auth.organization.service.ebo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zd.erp.auth.organization.dao.dao.OrganizationDao;
import cn.zd.erp.auth.organization.service.ebi.OrganizationEbi;
import cn.zd.erp.auth.organization.vo.Torganization;
import cn.zd.erp.auth.role.vo.Trole;
import cn.zd.erp.util.base.Organization;
import cn.zd.erp.util.base.PageFilter;
import cn.zd.erp.util.base.PageResults;
import cn.zd.erp.util.base.Role;
import cn.zd.erp.util.base.Tree;
@Transactional
@Service
public class OrganizationEbo implements OrganizationEbi {
	@Autowired
	private OrganizationDao dao;
	public void save(Torganization t) {
		dao.saveOrUpdate(t);
	}

	public void update(Torganization t) {
		
	}

	public void delete(Torganization t) {
		dao.delete(t);
	}

	public List<Torganization> getAll() {
		return null;
	}

	public Torganization get(Serializable uuid) {
		return dao.get(uuid);
	}

	public Torganization getByName(String name) {
		String hql = "from Torganization t where t.code=?";
		Torganization t = dao.getByHQL(hql, new String[] { name });
		return t;
	}

	public List<Torganization> dataGrid(Torganization t, PageFilter ph) {
		return null;
	}

	public Long count(Torganization t, PageFilter ph) {
		// TODO Auto-generated method stub
		return null;
	}

	public PageResults<Organization> dataGridCount(Torganization t,
			PageFilter ph) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 获得部门的树结构
	 * @return
	 */
	public List<Tree> getTree()
	{
		List<Tree> lt = new ArrayList<>();
		String hql ="select distinct t from Torganization t order by t.seq";
		List<Torganization> l = dao.getListByHQL(hql, null);
		if ((l != null) && (l.size() > 0)) {
			for (Torganization r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getOrganization() != null) {
					tree.setPid(r.getOrganization().getId().toString());
				}
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				lt.add(tree);
			}
		}
		return lt;
	}


	public PageResults<Organization> dataGridCount(Torganization t, int page,
		String sort, int rows, String order) {
		String hql = "select distinct t from Torganization t order by t." + sort + " "
				+ order;
		PageResults<Torganization> l = dao.findPageByFetchedHql(hql, null, page,
				rows, null);
		List<Organization> ul = new ArrayList<Organization>();
		for (Torganization b : l.getResults()) {
			Organization u = new Organization();
			BeanUtils.copyProperties(b, u);
			ul.add(u);
		}
		PageResults<Organization> pageResults = new PageResults<>();
		pageResults.setResults(ul);
		pageResults.setTotalCount(l.getTotalCount());
		return pageResults;
	}

	public List<Tree> getTree(Torganization t) {
		return null;
	}

}
